/*
 * AbstractGameEditorComponent.java
 *
 * Created on 14 de Julho de 2011, 17:58
 */

package intopo.game.gui;

import intopo.game.Strategy;

import java.awt.Color;
import java.io.Serializable;
import javax.swing.UIManager;

/**
 * An abstract class that represents a swing form that allows editing the parameters of a strategy of some game.
 *
 * <p> The description of a game must return a component that is used to show and edit a strategy parameters.
 * This class must implement two methods to get and set the strategy used by the component.
 * The same component instance may be used to display strategies with different parameters or of different classes.
 *
 * <p> Implementors must also provide a method that returns a textual description of the strategy to be used in a configuration file.
 *
 * @author  pedro
 */
abstract public class AbstractStrategyEditorComponent<
		S extends Strategy<S>>
	extends javax.swing.JPanel
	implements Serializable
{
	/**
	 * The strategy that is being edited and shown by this component.
	 */
	protected S strategy;
	/**
	 * Creates new form with no game.  If there are any components, they should show no values.
	 */
	protected AbstractStrategyEditorComponent ()
	{
		this.initComponents ();
	}
	/**
	 * Get a description of this game that can be written in a text configuration file.
	 * @return A description suitable for a text configuration file.
	 */
	abstract public String getConfigFileDescription ();
	/**
	 * Gets the strategy that is being edited and shown by this form.
	 * @return The strategy that is being edited and shown by this form.
	 * @throws Exception
	 */
	abstract public S getStrategy ()
		throws ParsingException;
	/**
	 * Sets the strategy that is being edited and/or shown by this form.  This method must update the components in this form.
	 * @param strategy The strategy that is being edited and/or shown by this form.
	 */
	abstract public void setStrategy (S strategy);
	/**
	 * Updates the colours of the text field component based on the given flag.
	 * @param textField The text field component to update.
	 * @param ok Ok flag.
	 */
	final protected void update (final javax.swing.JFormattedTextField textField, final boolean ok)
	{
		if (ok) {
			textField.setForeground (UIManager.getColor ("FormattedTextField.foreground"));
			textField.setBackground (UIManager.getColor ("FormattedTextField.background"));
		}
		else {
			textField.setForeground (Color.RED);
			textField.setBackground (Color.WHITE);
		}
	}
	final protected void updateInfoPanel (boolean isVisible)
	{
		this.infoPanel.setVisible (isVisible);
		if (isVisible) {
			try {
				this.binFormattedTextField.setValue (new Integer (this.getStrategy ().getBin ()));
			}
			catch (Exception e) {
				this.binFormattedTextField.setText (e.toString ());
			}
		}
	}
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      infoPanel = new javax.swing.JPanel();
      javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
      binFormattedTextField = new javax.swing.JFormattedTextField();

      setLayout(new java.awt.BorderLayout());

      infoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("information"));

      jLabel1.setText("Bin: ");
      infoPanel.add(jLabel1);

      binFormattedTextField.setColumns(5);
      binFormattedTextField.setEditable(false);
      infoPanel.add(binFormattedTextField);

      add(infoPanel, java.awt.BorderLayout.PAGE_END);
   }// </editor-fold>//GEN-END:initComponents
	
	
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JFormattedTextField binFormattedTextField;
   protected javax.swing.JPanel infoPanel;
   // End of variables declaration//GEN-END:variables
}
